import axios from 'axios'
import router from '/src/router/index.js'
import JSONBIG from 'json-bigint'
export default function request(config) {
    axios.defaults.baseURL = '/api'
    axios.defaults.headers.post['Content-Type'] = 'application/json';
    const instance = axios.create({
        baseURL: '/api',
        timeout: 10000,
        transformresponse: [data => {
            //data是原始字符串数据
            const jsonObject = JSONBIG.parse(data)
            return jsonObject
        }]
    })

    instance.interceptors.request.use(config => {
        const authorization = window.localStorage.getItem('authorization')
            //如果有登录用户则统一设置token
        if (authorization) {
            config.headers.Authorization = authorization
        } else {
            router.push('/login')
        }
        // console.log("请求成功")
        return config
    }, err => {
        // console.log("请求失败：" + err)
    })

    instance.interceptors.response.use(config => {
        console.log('响应成功')
        return config.data
    }, err => {
        // console.log("错误：" + err)
    })

    //发送真正的网络请求
    return instance(config)
}